package com.example.doublepink.mysqltest;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

public class AndroidMsql extends Activity {
    private EditText mTxtEdit,mTxtUrl,mTxtName,mTxtPsd;
    private Button mBtnSend;
    private TextView mTxtShow,mTxtErrShow;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        mTxtEdit = (EditText)findViewById(R.id.textEdit);
        mTxtShow = (TextView)findViewById(R.id.txtShow);
        mTxtUrl = (EditText)findViewById(R.id.txtUrlInput);
        mTxtName = (EditText)findViewById(R.id.txtName);
        mTxtPsd = (EditText)findViewById(R.id.txtPsd);
        mTxtErrShow = (TextView)findViewById(R.id.txtErrShow);
        mTxtUrl.setText("jdbc:mysql://10.202.9.89:8066/yamei?useUnicode=true&characterEncoding=utf8");
        mTxtName.setText("yamei");
        mTxtPsd.setText("123456");

        mBtnSend =(Button)findViewById(R.id.btn);
        mBtnSend.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                sqlCon();
            }
        });

    }

    private void sqlCon(){

        mTxtShow.setText("");
        mTxtErrShow.setText("");


        //在android中操作数据库最好在子线程中执行，否则可能会报异常
        new Thread()
        {
            public void run() {
                String str = mTxtEdit.getText().toString();
                Connection conn = null;
                try {
                    //注册驱动
                    Class.forName("com.mysql.jdbc.Driver");

                    String url =mTxtUrl.getText().toString().trim();
                    String name = mTxtName.getText().toString().trim();
                    String password = mTxtPsd.getText().toString().trim();

                    conn = DriverManager.getConnection(url, name, password);
                    String sql1 = "insert into tt_emoji(content) value(?)";
                    PreparedStatement stmt1 = conn.prepareStatement(sql1);
                    stmt1.setString(1, str);
                    int rs1 = stmt1.executeUpdate();
                    if (rs1 == 1) {
                        Log.i("", "写入成功");
                        stmt1.close();
                    } else {
                        Message msg = new Message();
                        msg.what = 2;
                        msg.obj = "写入失败";
                        handler.sendMessage(msg);

                        Log.i("", "连接失败");
                        return;
                    }

                    String sql2 = "select content from tt_emoji order by id desc limit 1";
                    PreparedStatement stmt2 = conn.prepareStatement(sql2);
                    ResultSet rs2 = stmt2.executeQuery();
                    if (rs2.next()) {
                        String newStr = rs2.getString("content");
                        Log.i("", "读取成功");

                        Message msg = new Message();
                        msg.what = 1;
                        msg.obj = newStr;
                        handler.sendMessage(msg);

                        rs2.close();
                        stmt2.close();
                    } else {
                        Message msg = new Message();
                        msg.what = 2;
                        msg.obj = "读取失败";
                        handler.sendMessage(msg);
                        Log.i("", "");
                        // ...
                        return;
                    }

                } catch (Exception e) {
                    Message msg = new Message();
                    msg.what = 2;
                    msg.obj = e.toString();
                    handler.sendMessage(msg);
                    Log.i("", e.toString());
                } finally {
                    try {
                        conn.close();
                    } catch (Exception e2) {
                        Message msg = new Message();
                        msg.what = 2;
                        msg.obj = e2.toString();
                        handler.sendMessage(msg);
                        Log.i("e", e2.toString());
                    }
                }
            };
        }.start();

    }

    private Handler handler = new Handler() {
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case 1: //成功
                    String str = String.valueOf(msg.obj);
                    mTxtShow.setText(str);

                    break;
                case 2: //失败
                    String strErr = String.valueOf(msg.obj);
                    mTxtErrShow.setText(strErr);

                    break;
                default:
                    break;
            }
        }
    };



}